Skip to content

Conversation

@psamanoelton
Copy link

Summary

  • Enable building TFQ against TensorFlow 2.16.2 with Bazel 6.5.x
  • Keep Keras 2 (“tf-keras”) path via TF_USE_LEGACY_KERAS=1 to avoid Keras 3 breakages
  • Allow selecting system/venv/conda Python; wire that interpreter through Bazel
  • Make wheel-build script honor chosen Python and include setuptools

Key changes

  • WORKSPACE: bump @org_tensorflow to 2.16.2, add a tiny @python local_repository shim
  • third_party/python_legacy/defs.bzl: export interpreter as a string (what TF’s local_config_python expects)
  • configure.sh: --python= flag; generate .tf_configure.bazelrc + .bazelrc + the python shim
  • .bazelrc: ensure TF_USE_LEGACY_KERAS=1 for build/test; keep existing warnings/rpath tweaks
  • release/build_pip_package.sh: invoke ${PYTHON_BIN_PATH:-python3} and ensure setuptools present
  • setup.py: (optionally) relax deps to run on TF 2.16 (sympy==1.14); extras pin tensorflow==2.16.0

Testing

  • Linux/Ubuntu 22.04 (Docker), Python 3.11, Bazelisk → Bazel 6.5.0
  • bazel build ... release:build_pip_package succeeds
  • Unit tests (scripts/test_all.sh) pass with
  • Wheel installs and imports with TF 2.16.2 + tf-keras 2.16.0, Cirq 1.3.0, NumPy 1.26.4

Notes

  • This keeps TFQ on Keras 2 path; Keras 3 migration can be handled in a a future PR.
  • This changes will help on the incoming upgrades to 2.20.

@mhucka mhucka self-assigned this Nov 11, 2025
@mhucka mhucka added area/dependencies Involves libraries or other software that TFQ depends on area/health Involves general matters of project configuration, health, maintenance, and similar concerns area/installation Involves installation of TFQ area/tensorflow Involves TensorFlow labels Nov 11, 2025
Copy link
Member

@mhucka mhucka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the work, and I'm sorry about the number of changes requested here …

fi

mkdir -p ${DEST}
mkdir -p "${DEST}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trivial nit: something happened to the intendentation on this line.

Suggested change
mkdir -p "${DEST}"
mkdir -p "${DEST}"

Comment on lines +103 to +105
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should omit these, since these versions are no longer actively supported.

Suggested change
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",

"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add 3.12 and 3.13 (assuming the updated TFQ from this PR works in those versions):

Suggested change
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",

Comment on lines +87 to +88
author="Google Inc.",
author_email="no-reply@google.com",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this file is getting changes for other reasons, it may as well also include an update to these values. The general approach we've taken in other major QAI projects is to use the following pattern, so I think it's worth including in this PR so that another PR isn't needed to make this change later.

Suggested change
author="Google Inc.",
author_email="no-reply@google.com",
author="The TensorFlow Quantum Authors",
author_email="tensorflow-quantum-team@google.com",

# pylint: enable=undefined-variable

__version__ = '0.7.2'
__version__ = '0.7.5'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, sorry I didn't notice this before: the next version is actually 0.7.4 (since the current version on PyPI is 0.7.3):

Suggested change
__version__ = '0.7.5'
__version__ = '0.7.4'


function is_windows() {
# On windows, the shell script is actually running in msys
is_windows() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
is_windows() {
function is_windows() {


function is_ppc64le() {
[[ "$(uname -m)" == "ppc64le" ]]
write_legacy_python_repo() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
write_legacy_python_repo() {
function write_legacy_python_repo() {

Comment on lines +114 to +118
PY_ABS="$("${PY}" - <<'PY'
import os,sys
print(os.path.abspath(sys.executable))
PY
)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this works, IMHO this construct is fragile because of the complicated syntax. (At first, I thought the )" by itself at the end was a typo.) Would the following not work?

Suggested change
PY_ABS="$("${PY}" - <<'PY'
import os,sys
print(os.path.abspath(sys.executable))
PY
)"
PY_ABS="$("${PY}" -c 'import os,sys; print(os.path.abspath(sys.executable))')"

Comment on lines +144 to +175
"${PYTHON_BIN_PATH}" - <<'PY' || { echo "ERROR: tensorflow not importable by chosen Python."; exit 1; }
import tensorflow as tf
import tensorflow.sysconfig as sc
print("TF:", tf.__version__)
print("include:", sc.get_include())
print("lib:", sc.get_lib())
PY

# --- discover TF include/lib robustly --------------------------------------
HDR="$("$PYTHON_BIN_PATH" - <<'PY'
import tensorflow.sysconfig as sc
print(sc.get_include())
PY
)"

LIBDIR="$("$PYTHON_BIN_PATH" - <<'PY'
import os, tensorflow.sysconfig as sc
p = sc.get_lib()
print(p if os.path.isdir(p) else os.path.dirname(p))
PY
)"

LIBNAME="$("$PYTHON_BIN_PATH" - <<'PY'
import os, glob, tensorflow.sysconfig as sc
p = sc.get_lib()
d = p if os.path.isdir(p) else os.path.dirname(p)
cands = glob.glob(os.path.join(d,'libtensorflow_framework.so*')) \
or glob.glob(os.path.join(d,'libtensorflow.so*')) \
or glob.glob(os.path.join(d,'_pywrap_tensorflow_internal.*'))
print(os.path.basename(cands[0]) if cands else 'libtensorflow_framework.so.2')
PY
)"
Copy link
Member

@mhucka mhucka Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The execution of this is slow because it imports tensorflow 4 times. It's true that the imports after the first time will be faster thanks to caching by the operating system, but still, it feels like there must be a more efficient way than running Python 4 times. Also, there is a little bit of duplication of code.

What about the following?

Suggested change
"${PYTHON_BIN_PATH}" - <<'PY' || { echo "ERROR: tensorflow not importable by chosen Python."; exit 1; }
import tensorflow as tf
import tensorflow.sysconfig as sc
print("TF:", tf.__version__)
print("include:", sc.get_include())
print("lib:", sc.get_lib())
PY
# --- discover TF include/lib robustly --------------------------------------
HDR="$("$PYTHON_BIN_PATH" - <<'PY'
import tensorflow.sysconfig as sc
print(sc.get_include())
PY
)"
LIBDIR="$("$PYTHON_BIN_PATH" - <<'PY'
import os, tensorflow.sysconfig as sc
p = sc.get_lib()
print(p if os.path.isdir(p) else os.path.dirname(p))
PY
)"
LIBNAME="$("$PYTHON_BIN_PATH" - <<'PY'
import os, glob, tensorflow.sysconfig as sc
p = sc.get_lib()
d = p if os.path.isdir(p) else os.path.dirname(p)
cands = glob.glob(os.path.join(d,'libtensorflow_framework.so*')) \
or glob.glob(os.path.join(d,'libtensorflow.so*')) \
or glob.glob(os.path.join(d,'_pywrap_tensorflow_internal.*'))
print(os.path.basename(cands[0]) if cands else 'libtensorflow_framework.so.2')
PY
)"
tf_output=$("${PYTHON_BIN_PATH}" - <<'PY'
import sys
import os
import glob
try:
import tensorflow as tf
import tensorflow.sysconfig as sc
except ImportError:
sys.exit(1)
print(sc.get_include())
lib_path = sc.get_lib()
lib_dir = lib_path if os.path.isdir(lib_path) else os.path.dirname(lib_path)
print(lib_dir)
cands = (glob.glob(os.path.join(lib_dir, 'libtensorflow_framework.so*')) or
glob.glob(os.path.join(lib_dir, 'libtensorflow.so*')) or
glob.glob(os.path.join(lib_dir, '_pywrap_tensorflow_internal.*')))
print(os.path.basename(cands[0]) if cands else 'libtensorflow_framework.so.2')
PY
)
if [[ $? -ne 0 ]]; then
echo "ERROR: tensorflow not importable by Python (${PYTHON_BIN_PATH})" >&2
exit 1
fi
{
read -r HDR
read -r LIBDIR
read -r LIBNAME
} <<< "${tf_output}"


load("@org_tensorflow//tensorflow:workspace0.bzl", "tf_workspace0")

tf_workspace0()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the removal of this call to tf_workspace0() intentional?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/dependencies Involves libraries or other software that TFQ depends on area/health Involves general matters of project configuration, health, maintenance, and similar concerns area/installation Involves installation of TFQ area/tensorflow Involves TensorFlow

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants